package com.todod.base.module.base.service.rtdistribute;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.todod.base.module.base.controller.admin.rtdistribute.vo.*;
import com.todod.base.module.base.dal.dataobject.rtdistribute.RtDistributeDO;
import com.todod.base.framework.common.pojo.PageResult;
import com.todod.base.framework.common.pojo.PageParam;
import com.todod.base.framework.common.util.object.BeanUtils;

import com.todod.base.module.base.dal.mysql.rtdistribute.RtDistributeMapper;

import static com.todod.base.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.todod.base.module.base.enums.ErrorCodeConstants.*;

/**
 * 实时数据分发 Service 实现类
 *
 * @author 超管儿
 */
@Service
@Validated
public class RtDistributeServiceImpl implements RtDistributeService {

    @Resource
    private RtDistributeMapper rtDistributeMapper;

    @Override
    public Long createRtDistribute(RtDistributeSaveReqVO createReqVO) {
        // 插入
        RtDistributeDO rtDistribute = BeanUtils.toBean(createReqVO, RtDistributeDO.class);
        rtDistributeMapper.insert(rtDistribute);
        // 返回
        return rtDistribute.getId();
    }

    @Override
    public void updateRtDistribute(RtDistributeSaveReqVO updateReqVO) {
        // 校验存在
        validateRtDistributeExists(updateReqVO.getId());
        // 更新
        RtDistributeDO updateObj = BeanUtils.toBean(updateReqVO, RtDistributeDO.class);
        rtDistributeMapper.updateById(updateObj);
    }

    @Override
    public void deleteRtDistribute(Long id) {
        // 校验存在
        validateRtDistributeExists(id);
        // 删除
        rtDistributeMapper.deleteById(id);
    }

    private void validateRtDistributeExists(Long id) {
        if (rtDistributeMapper.selectById(id) == null) {
            throw exception(RT_DISTRIBUTE_NOT_EXISTS);
        }
    }

    @Override
    public RtDistributeDO getRtDistribute(Long id) {
        return rtDistributeMapper.selectById(id);
    }

    @Override
    public PageResult<RtDistributeDO> getRtDistributePage(RtDistributePageReqVO pageReqVO) {
        return rtDistributeMapper.selectPage(pageReqVO);
    }

}